package com.yaozheng.vocationaltraining.utils;

/**
 * Created by xiaoqing on 2017/3/1.
 */
public class NumberUtils {
    /**
     * string 转int
     *
     * @param number
     * @param defaultValue
     * @return
     */
    public static int stringToInt(String number, int defaultValue) {
        if (number == null) {
            return defaultValue;
        }
        if ("".equals(number)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(number);
        } catch (Exception ex) {
            return defaultValue;
        }
    }


    public static int stringToInt(String number) {
        return stringToInt(number, -1);
    }


    /**
     * string 转int
     *
     * @param number
     * @param defaultValue
     * @return
     */
    public static boolean stringToBoolen(String number, boolean defaultValue) {
        if (number == null) {
            return defaultValue;
        }
        if ("".equals(number)) {
            return defaultValue;
        }
        try {
            return Boolean.parseBoolean(number);
        } catch (Exception ex) {
            return defaultValue;
        }
    }


    public static boolean stringToBoolen(String number) {
        return stringToBoolen(number, false);
    }

    /**
     * 是否数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * string 转 long
     *
     * @param number
     * @param defaultValue
     * @return
     */
    public static long stringToLong(String number, long defaultValue) {
        try {
            return Long.parseLong(number);
        } catch (Exception ex) {
            ErrorUtils.outErrorLog(ex);
            return defaultValue;
        }
    }


    public static long stringToLong(String number) {
        return stringToLong(number, -1);
    }
}
